package mylittleinvestigations.GameUi;

import java.util.logging.Level;
import java.util.logging.Logger;
import mylittleinvestigations.PersistentThreads.RenderThread;
import mylittleinvestigations.*;
import org.dawnmew.jvirtualstream.VirtualFileSystem;
import org.lwjgl.LWJGLException;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Image;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.UnicodeFont;

public class GameUiLoader implements Runnable
{
    private static GameUiLoader instance = null;
    
    public static GameUiLoader getInstance()
    {
        if (instance == null)
        {
            instance = new GameUiLoader();
        }
        
        return instance;
    }
    
    public volatile UnicodeFont dialogFont;
    public UnicodeFont tabFont;
    public UnicodeFont evidenceSelectorFont;
    public UnicodeFont confrontationNumberingFont;
    public UnicodeFont partnerInformationFont;
    
    private Thread uiLoadingThread;
    
    private GameUiLoader()
    {
        this.isUiLoaded = false;
    }
    
    public UnicodeFont getDialogFont() { return this.dialogFont; }
    
    private volatile boolean isDialogFontLoaded;
    public boolean getIsDialogFontLoaded() { return this.isDialogFontLoaded; }
    
    public volatile boolean isUiLoaded;
    public boolean getIsUiLoaded() { return this.isUiLoaded; }
    
    public void LoadUi()
    {
        if (this.uiLoadingThread == null)
        {
            this.uiLoadingThread = new Thread(this);
            this.uiLoadingThread.setName("MLI UI Loading Thread");
            this.uiLoadingThread.setPriority(Thread.MIN_PRIORITY);
            this.uiLoadingThread.start();
        }
    }

    public void run()
    {
        try
        {
            /*while (!RenderThread.getInstance().IsReady())
            {
                try
                {
                    Thread.sleep(10);
                }
                catch (InterruptedException ex)
                {
                    Logger.getLogger(GameUiLoader.class.getName()).log(Level.SEVERE, null, ex);
                }
            }*/
                
            // We'll try to make the shared context current,
            // but this may fail if the player has integrated graphics.
            // In that case, we'll turn off the use of the shared context
            // and will load everything on the UI thread in that case.
            try
            {
                if(Settings.UseSharedContext)
                {
                    RenderThread.getSharedContext().makeCurrent();
                    System.out.println("OpenGL shared context support is functional.");
                }else{
                    System.out.println("OpenGL shared context support is disabled.");
                }
            }
            catch (LWJGLException e)
            {
                Settings.UseSharedContext = false;
                if(RenderThread.getSharedContext()!=null)
                {
                    RenderThread.getSharedContext().releaseContext();
                }
                System.out.println("OpenGL shared context support isn't functional. Falling back to single-context mode.");
            }

            this.isDialogFontLoaded = true;

            if (Settings.UseSharedContext)
            {
                Arrow.Initialize(new Image(VirtualFileSystem.getInstance().getInputStream("image/ArrowHorizontal.png"), "image/ArrowHorizontal.png", false), new Image(VirtualFileSystem.getInstance().getInputStream("image/ArrowVertical.png"), "image/ArrowVertical.png", false));

                // +3 to overlap the tab's border with the dialog background's border.
                Tab.Initialize((int)Settings.DisplayBounds.getY() - Dialog.Height - Tab.Height + 3, (int)Settings.DisplayBounds.getY() - Tab.Height, new Image(VirtualFileSystem.getInstance().getInputStream("image/Tab.png"),"image/Tab.png", false), new Image(VirtualFileSystem.getInstance().getInputStream("image/TabTop.png"), "image/TabTop.png", false), tabFont);
                Button.Initialize(dialogFont, new Image(VirtualFileSystem.getInstance().getInputStream("image/CheckMark.png"), "image/CheckMark.png", false), new Image(VirtualFileSystem.getInstance().getInputStream("image/Lock.png"), "image/Lock.png", false));

                ButtonArray.Initialize(
                    3, 370, 954, 167,
                    30,
                    new Image(VirtualFileSystem.getInstance().getInputStream("image/EncounterPrompt.png"), "image/EncounterPrompt.png", false),
                    this.tabFont,
                    this.dialogFont);

                EvidenceSelector.Initialize(
                    new Image(VirtualFileSystem.getInstance().getInputStream("image/EvidenceSelectorItem.png"), "image/EvidenceSelectorItem.png", false),
                    new Image(VirtualFileSystem.getInstance().getInputStream("image/EvidenceSelectorItemSelection.png"), "image/EvidenceSelectorItemSelection.png", false),
                    new Image(VirtualFileSystem.getInstance().getInputStream("image/EvidenceSelectorDescription.png"), "image/EvidenceSelectorDescription.png", false),
                    this.evidenceSelectorFont);

                Dialog.Initialize(
                    3, 370, 954, 167,
                    30,
                    this.dialogFont);

                Notification.Initialize(
                    3, 363, 954, 174,
                    30,
                    this.dialogFont);

                Confrontation.Initialize(
                    new Image(VirtualFileSystem.getInstance().getInputStream("image/ConfrontationHealthCircle.png"), "image/ConfrontationHealthCircle.png", false),
                    this.confrontationNumberingFont);
                Encounter.Initialize(
                    new Image(VirtualFileSystem.getInstance().getInputStream("image/DialogBackground.png"), "image/DialogBackground.png", false),
                    new Image(VirtualFileSystem.getInstance().getInputStream("image/EncounterDarkBackground.png"), "image/EncounterDarkBackground.png", false));
                PartnerInformation.Initialize(
                    new Image(VirtualFileSystem.getInstance().getInputStream("image/PartnerInfoBackground.png"), "image/PartnerInfoBackground.png",false),
                    this.tabFont,
                    this.partnerInformationFont);

                RenderThread.getSharedContext().releaseContext();
                this.isUiLoaded = true;
            }
            else
            {
                RenderThread.getInstance().LoadGameUi();
                RenderThread.getInstance().FinalizeImagesToLoad();
            }
        }
        catch (SlickException ex)
        {
            Logger.getLogger(GameUiLoader.class.getName()).log(Level.SEVERE, null, ex);
        }
        catch (LWJGLException ex)
        {
            Logger.getLogger(GameUiLoader.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
